<?php

namespace App\Http\Controllers\Agent;

use App\Exceptions\BizException;
use App\Http\Controllers\Controller;
use App\Model\Banner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class BannerController extends Controller
{
    public $agent;

    public function __construct()
    {
        $this->middleware(['agent']);
        $this->agent = Auth::guard('agent')->user();
    }

    public function index(Request $request)
    {
        $params = $request->all();
        $model = Banner::where('status', '>', '-1')->where('agent_id', $this->agent->id);
        $list = $model->orderBy('order_num', 'desc')->orderBy('id', 'desc')->paginate(15);
        return view('agent.banner.index', [
            'list' => $list,
            'params' => $params,
        ]);
    }

    public function add(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            try {
                $this->validate($request, [
                    'pic' => 'required',
                ], [
                    'pic.required' => '请上传图片',
                ]);

                $params = $request->all();
                $params['agent_id'] = $this->agent->id;
                Banner::create($params);
                return response()->json(['status' => 1, 'info' => '保存成功']);
            } catch (ValidationException $e) {
                return response()->json(['status' => 0, 'info' => $e->validator->errors()->first()]);
            } catch (BizException $e) {
                return response()->json(['status' => 0, 'info' => $e->getMessage()]);
            }
        } else {
            return view('agent.banner.add');
        }
    }

    public function edit(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            try {
                $this->validate($request, [
                    'pic' => 'required',
                ], [
                    'pic.required' => '请上传图片',
                ]);

                $params = $request->all();
                $id = $params['id'];

                if (isset($params['id']))
                    unset($params['id']);
                Banner::where('id', $id)->update($params);
                return response()->json(['status' => 1, 'info' => '保存成功']);
            } catch (ValidationException $e) {
                return response()->json(['status' => 0, 'info' => $e->validator->errors()->first()]);
            } catch (BizException $e) {
                return response()->json(['status' => 0, 'info' => $e->getMessage()]);
            }
        } else {
            $row = Banner::find($request->input('id'));
            if ($row['agent_id'] != $this->agent->id)
                return '你无权修改';

            return view('agent.banner.edit', ['row' => $row]);
        }
    }

    public function del(Request $request)
    {
        Banner::where('id', $request->input('id'))->update(['status' => -1]);
        return response()->json(['status' => 1, 'info' => '删除成功']);
    }
}